﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using omsData;
using System.Web.Configuration;

namespace myomscl_web.Admin.UserControls
{
    public partial class ucListUserScreen : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void LoadListScreenData()
        {
            this.btnCreateNewListscreen.Visible = false;
            this.divListUserScreen.Visible = true;

            int id = Int32.Parse(SelectedListScreenId);
            using (OmsModelContainer context = new OmsModelContainer())
            {

                var listScreen = context.ListScreens.Where(ls => ls.Id == id).First();
                this.txtName.Text = listScreen.Name;
                this.txtDescription.Text = listScreen.Description;
                this.txtTitle.Text = listScreen.Title;
                this.chkExportEnabled.Checked = listScreen.ExportEnabled;
                this.chkFilterEnabled.Checked = listScreen.FilterEnabled;
                this.chkCounterEnabled.Checked = listScreen.CounterEnabled;

                var selectedCols = listScreen.ListColumns.ToList();
                var allCols = context.ListColumns.ToList();
                var unselectedCols = allCols.Except(selectedCols).ToList();
               
                this.ucShift1.LoadSources(unselectedCols.ConvertAll(new Converter<ListColumns, ListItem>(ListColumnToListItem)),
                    selectedCols.ConvertAll(new Converter<ListColumns, ListItem>(ListColumnToListItem)));
            }

        }

        private ListItem ListColumnToListItem(ListColumns col)
        {
            return new ListItem(col.Name, col.Id.ToString());
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            LoadScreen();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            using (OmsModelContainer context = new OmsModelContainer())
            {

                ListScreens listScreen;

                if (IsNewListScreen)
                {
                    listScreen = new ListScreens();
                    context.AddToListScreens(listScreen);
                }
                else
                {
                    int id = Int32.Parse(SelectedListScreenId);
                    listScreen = context.ListScreens.Where(ls => ls.Id == id).First();                    
                }

                listScreen.Name = this.txtName.Text;
                listScreen.Description = this.txtDescription.Text;
                listScreen.Title = this.txtTitle.Text;
                listScreen.FilterEnabled = this.chkFilterEnabled.Checked;
                listScreen.CounterEnabled = this.chkCounterEnabled.Checked;
                listScreen.ExportEnabled = this.chkExportEnabled.Checked;

                var cols = this.ucShift1.GetSelected();
                listScreen.ListColumns.Clear();
                foreach (ListItem col in cols)
                {
                    int id = Int32.Parse(col.Value);
                    var c = context.ListColumns.Where(cc => cc.Id == id).First();
                    listScreen.ListColumns.Add(c);
                }

                context.SaveChanges();
            }

            LoadScreen();
        }

        protected void btnCreateNewListscreen_Click(object sender, EventArgs e)
        {
            this.btnCreateNewListscreen.Visible = false;
            this.divListUserScreen.Visible = true;

        }

        public bool IsNewListScreen
        {
            get
            {
                if (Session["IsNewListScreen"] == null)
                    return false;
                return (bool)Session["IsNewListScreen"];
            }
            set { Session["IsNewListScreen"] = value; }
        }

        public string SelectedListScreenId
        {
            get
            {
                if (Session["SelectedListScreenId"] != null)
                    return Session["SelectedListScreenId"].ToString();
                return null;
            }
            set
            {
                Session["SelectedListScreenId"] = value;
            }
        }

        public void LoadScreen()
        {
            if (this.IsNewListScreen)
            {
                this.btnCreateNewListscreen.Visible = true;
                this.divListUserScreen.Visible = false;

                using (OmsModelContainer context = new OmsModelContainer())
                {
                    var listColumns = context.ListColumns.ToList();
                    List<ListItem> cols = new List<ListItem>();
                    foreach(var col in listColumns)
                    {
                        cols.Add(new ListItem(col.Name,col.Id.ToString()));
                    }
                    ucShift1.LoadAll(cols);
                }
            }
            else
            {
                LoadListScreenData();
            }
        }
    }
}